Amazon平台SP

您所在的位置:网站首页 api sp和api sl Amazon平台SP

Amazon平台SP

2024-07-12 13:23:22| 来源: 网络整理| 查看: 265

Amazon平台SP-API的SDK包生成和使用详解 目前包含了卖家授权Token、订单Orders、财务Finances、FBA库存FBAInventory、创建Feed文档、加密上传和下载并解密文档等模块的SDK包。 一、生成授权Token的SDK 二、生成订单Orders的SDK 三、生成财务Finances的SDK 四、生成FBA库存FBAInventory的SDK 五、生成创建Feed文档的SDK 六、生成加密上传和下载并解密文档的SDK 七、创建Feed文档和加密上传的组合使用示例 八、已生成好的SDK包(可直接下载使用)

目前包含了卖家授权Token、订单Orders、财务Finances、FBA库存FBAInventory、创建Feed文档、加密上传和下载并解密文档等模块的SDK包。

介绍下我当时对接亚马逊平台API时生成SDK包的一些操作,给需要用到的同学一些指引. 文章后面附带有已生成的SDK包的下载链接,大家可直接下载使用。

各位小伙伴,如果觉得该文章对你有帮助,请帮忙点个赞,谢谢。

生成SDK的官方文档介绍: https://developer-docs.amazon.com/sp-api/docs/generating-a-java-sdk-with-lwa-token-exchange-and-authentication 按照官方文档的说明,一步步操作:

本地环境安装jdk、maven、wget(已安装过的请忽略); 进入官方源码项目selling-partner-api-models,将项目源码下载到本地(项目源码链接:https://github.com/amzn/selling-partner-api-models)。我是放在桌面(C:\Users\administer\Desktop\selling-partner-api-models-main); 下载Swagger代码生成器(可使用Wget命令:wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.13/swagger-codegen-cli-2.4.13.jar -O swagger-codegen-cli.jar); 在C盘新建文件夹SwaggerToCL, 将下载的swagger-codegen-cli.jar复制到该文件夹中(C:\SwaggerToCL\swagger-codegen-cli.jar); 一、生成授权Token的SDK 打包:进入目录 C:\Users\administer\Desktop\selling-partner-api-models-main\clients\sellingpartner-api-aa-java,从此处进入cmd命令行窗口(或者先打开cmd命令行窗口再切换路径), 执行 mvn package 命令打包。 安装:打包成功无报错后,进入target文件夹 ,查看是否有新生成的完整的sellingpartnerapi-aa-java-1.0-jar-with-dependencies.jar。若有,(已下载SDK的可直接从该步开始,前面可忽略)则进入cmd执行命令在本地 Maven 存储库中安装。 (命令:mvn install:install-file -Dfile=sellingpartnerapi-aa-java-1.0-jar-with-dependencies.jar -DgroupId=com.amazon.sellingpartnerapi -DartifactId=sellingpartnerapi-aa-java -Dversion=1.0 -Dpackaging=jar)。安装成功后,在本地maven仓库中对应目录下就能看到该sdk包了(如:C:\Users\administer\.m2\repository\com\amazon\sellingpartnerapi\sellingpartnerapi-aa-java\1.0\sellingpartnerapi-aa-java-1.0.jar)。 添加依赖:在对应项目的pom.xml中添加maven依赖: com.amazon.sellingpartnerapi sellingpartnerapi-aa-java 1.0 代码调用: 官方文档:https://developer-docs.amazon.com/sp-api/docs/connecting-to-the-selling-partner-api-using-a-generated-java-sdk

示例代码: AmazonSPSellerRequestModel 主要是放授权需要用的一些公共参数。 如accessKeyId,secretKey,region等等

public class AmazonSPSellerRequestModel { /** Your AWS access key Id. For more information */ private String accessKeyId; /** Your AWS secret access key. For more information */ private String secretKey; /** The AWS region to which you are directing your call */ private String region; /** The ARN of the IAM role */ private String roleArn; /** An identifier for the session that you define. We recommend using a Universally Unique Identifier (UUID) */ private String roleSessionName = UUID.randomUUID().toString(); /** Your LWA client identifier */ private String clientId; /** Your LWA client secret */ private String clientSecret; /** The LWA refresh token */ private String refreshToken; /** The LWA authentication server URI */ private String endpoint; /** 是否GrantlessOperation */ private boolean isGrantlessOperation; /** 速率限制 */ private Double rateLimitPermit; } public class AmazonSPSellerConfig { // 正式环境token地址 private static final String PROD_TOKEN_URL = "https://api.amazon.com/auth/o2/token"; /** * Configure your AWS credentials * * @param requestModel * @return * @throws Exception */ public static AWSAuthenticationCredentials getAWSAuthenticationCredentials(AmazonSPSellerRequestModel requestModel) throws Exception { AWSAuthenticationCredentials awsAuthenticationCredentials = AWSAuthenticationCredentials.builder() .accessKeyId(requestModel.getAccessKeyId()) .secretKey(requestModel.getSecretKey()) .region(requestModel.getRegion()) .build(); return awsAuthenticationCredentials; } /** * Configure your AWS credentials provider * * @param requestModel * @return * @throws Exception */ public static AWSAuthenticationCredentialsProvider getAWSAuthenticationCredentialsProvider (AmazonSPSellerRequestModel requestModel) throws Exception { AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider = AWSAuthenticationCredentialsProvider.builder() .roleArn(requestModel.getRoleArn()) .roleSessionName(requestModel.getRoleSessionName()) .build(); return awsAuthenticationCredentialsProvider; } /** * Configure your LWA credentials(not grantless operations) * * @param requestModel * @return * @throws Exception */ public static LWAAuthorizationCredentials getLWAAuthorizationCredentials4NotGrantlessOperation(AmazonSPSellerRequestModel requestModel) throws Exception { LWAAuthorizationCredentials lwaAuthorizationCredentials = LWAAuthorizationCredentials.builder() .clientId(requestModel.getClientId()) .clientSecret(requestModel.getClientSecret()) .refreshToken(requestModel.getRefreshToken()) .endpoint(PROD_TOKEN_URL) .build(); return lwaAuthorizationCredentials; } /** * Configure your LWA credentials(grantless operations) * * @param requestModel * @return * @throws Exception */ public static LWAAuthorizationCredentials getLWAAuthorizationCredentials4GrantlessOperation(AmazonSPSellerRequestModel requestModel) throws Exception { LWAAuthorizationCredentials lwaAuthorizationCredentials = LWAAuthorizationCredentials.builder() .clientId(requestModel.getClientId()) .clientSecret(requestModel.getClientSecret()) .withScopes(ScopeConstants.SCOPE_NOTIFICATIONS_API, ScopeConstants.SCOPE_MIGRATION_API) .endpoint(PROD_TOKEN_URL) .build(); return lwaAuthorizationCredentials; } /** * Configure your LWA credentials * * @param requestModel * @return * @throws Exception */ public static LWAAuthorizationCredentials getLWAAuthorizationCredentials(AmazonSPSellerRequestModel requestModel) throws Exception { if (requestModel.getIsGrantlessOperation()) { // 是无授权操作 return getLWAAuthorizationCredentials4GrantlessOperation(requestModel); } else { // 非无授权操作 return getLWAAuthorizationCredentials4NotGrantlessOperation(requestModel); } } } 二、生成订单Orders的SDK

复制文件:进入项目目录 C:\Users\administer\Desktop\selling-partner-api-models-main,选择models文件夹并进入, 找到订单项目的文件夹orders-api-model,进入文件夹将 ordersV0.json 文件复制到 C:\SwaggerToCL。(C:\Users\administer\Desktop\selling-partner-api-models-main\models\orders-api-model)

生成项目代码:进入目录 C:\SwaggerToCL ,进入cmd, 执行命令通过Swagger代码生成器生成Order项目(java -jar C:\SwaggerToCL\swagger-codegen-cli.jar generate -i C:\SwaggerToCL\ordersV0.json -l java -t C:\Users\administer\Desktop\selling-partner-api-models-main\clients\sellingpartner-api-aa-java\resources\swagger-codegen\templates\ -o C:\SwaggerToCL\Orders_JavaCL),可以看到新生成的订单项目文件夹Orders_JavaCL。

添加依赖:进入目录C:\SwaggerToCL\Orders_JavaCL,在pom.xml文件中添加依赖: com.amazon.sellingpartnerapi sellingpartnerapi-aa-java 1.0

打包:进入目录 C:\SwaggerToCL\Orders_JavaCL,进入cmd, 执行 mvn package 命令打包。

安装:打包成功无报错后,进入target文件夹 ,查看是否有新生成的完整的swagger-java-client-1.0.0.jar。若有,则改jar名为:sellingpartnerapi-aa-java-orders-1.0.jar。 (已下载SDK的可直接从该步开始,前面可忽略)然后进入cmd执行命令在本地 Maven 存储库中安装。 (命令:mvn install:install-file -Dfile=sellingpartnerapi-aa-java-orders-1.0.jar -DgroupId=com.amazon.sellingpartnerapi -DartifactId=orders -Dversion=1.0 -Dpackaging=jar)。安装成功后,在本地maven仓库中对应目录下就能看到该sdk包了(如:C:\Users\administer\.m2\repository\com\amazon\sellingpartnerapi\orders\1.0\orders-1.0.jar)。

添加依赖:在对应项目的pom.xml中添加maven依赖: com.amazon.sellingpartnerapi orders 1.0

代码调用: 官方文档:https://developer-docs.amazon.com/sp-api/docs/connecting-to-the-selling-partner-api-using-a-generated-java-sdk

示例代码:

/** * 获取速率限制配置 * * @param requestModel * @return * @throws Exception */ public static RateLimitConfiguration getRateLimitConfiguration (AmazonSPSellerRequestModel requestModel) throws Exception { RateLimitConfiguration rateLimitConfiguration = RateLimitConfigurationOnRequests.builder() .rateLimitPermit(requestModel.getRateLimitPermit()) .waitTimeOutInMilliSeconds(30000L) .build(); return rateLimitConfiguration; } /** * Create an instance of the Orders API and call an operation * * @param requestModel * @return * @throws Exception */ public static OrdersV0Api getOrdersApiInstance(AmazonSPSellerRequestModel requestModel) throws Exception { OrdersV0Api apiInstance = new OrdersV0Api.Builder() .awsAuthenticationCredentials(getAWSAuthenticationCredentials(requestModel)) .lwaAuthorizationCredentials(getLWAAuthorizationCredentials(requestModel)) .awsAuthenticationCredentialsProvider(getAWSAuthenticationCredentialsProvider(requestModel)) .rateLimitConfigurationOnRequests(getRateLimitConfiguration(requestModel)) .endpoint(requestModel.getEndpoint()) .build(); return apiInstance; } /** * 获取订单 * 请求速率: 默认0.0167/s;最大请求数:20 * * @param requestModel * @return */ // 部分示例代码:requestModel 主要是放授权需要用的一些公共参数。 如accessKeyId,secretKey,region等等 double permitsPerSecond = 0.0167; requestModel.setRateLimitPermit(permitsPerSecond); OrdersV0Api apiInstance = AmazonSPSellerConfig.getOrdersApiInstance(requestModel); GetOrdersResponse response = apiInstance.getOrders(marketplaceIds, null, null, lastUpdatedAfter, lastUpdatedBefore, null, fulfillmentChannels, null, null, null, maxResultsPerPage, null, nextToken, amazonOrderIds, null, null, null); /** * 获取订单明细列表 * 请求速率: 默认0.5/s;最大请求数:30 * * @param requestModel * @param orderId * @return */ // 部分示例代码:requestModel 主要是放授权需要用的一些公共参数。 如accessKeyId,secretKey,region等等 double permitsPerSecond = 0.5; requestModel.setRateLimitPermit(permitsPerSecond); OrdersV0Api apiInstance = AmazonSPSellerConfig.getOrdersApiInstance(requestModel); GetOrderItemsResponse response = apiInstance.getOrderItems(orderId, nextToken); 三、生成财务Finances的SDK

操作流程和生成订单Orders的SDK相同,只是目录地址及项目文件夹不同。

复制文件:进入目录C:\Users\administer\Desktop\selling-partner-api-models-main\models, 找到财务项目的文件夹finances-api-model,进入文件夹将 financesV0.json文件复制到 C:\SwaggerToCL。(C:\Users\administer\Desktop\selling-partner-api-models-main\models\finances-api-model)

生成项目代码:进入目录 C:\SwaggerToCL ,进入cmd, 执行命令通过Swagger代码生成器生成财务项目(java -jar C:\SwaggerToCL\swagger-codegen-cli.jar generate -i C:\SwaggerToCL\financesV0.json -l java -t C:\Users\administer\Desktop\selling-partner-api-models-main\clients\sellingpartner-api-aa-java\resources\swagger-codegen\templates\ -o C:\SwaggerToCL\Finances_JavaCL),可以看到新生成的财务项目文件夹Finances_JavaCL。

添加依赖:进入目录C:\SwaggerToCL\Finances_JavaCL,在pom.xml文件中添加依赖: com.amazon.sellingpartnerapi sellingpartnerapi-aa-java 1.0

打包:进入目录 C:\SwaggerToCL\Finances_JavaCL,进入cmd, 执行 mvn package 命令打包。

安装:打包成功无报错后,进入target文件夹 ,查看是否有新生成的完整的swagger-java-client-1.0.0.jar。若有,则改jar名为:sellingpartnerapi-aa-java-finances-1.0.jar。 (已下载SDK的可直接从该步开始,前面可忽略)然后进入cmd执行命令在本地 Maven 存储库中安装。 (命令:mvn install:install-file -Dfile=sellingpartnerapi-aa-java-finances-1.0.jar -DgroupId=com.amazon.sellingpartnerapi -DartifactId=finances -Dversion=1.0 -Dpackaging=jar)。安装成功后,在本地maven仓库中对应目录下就能看到该sdk包了(如:C:\Users\administer\.m2\repository\com\amazon\sellingpartnerapi\finances\1.0\finances-1.0.jar)。

添加依赖:在对应项目的pom.xml中添加maven依赖: com.amazon.sellingpartnerapi finances 1.0

代码调用: 示例代码:

/** * Create an instance of the Finances API and call an operation * * @param requestModel * @return * @throws Exception */ public static DefaultApi getFinancesApiInstance(AmazonSPSellerRequestModel requestModel) throws Exception { DefaultApi apiInstance = new DefaultApi.Builder() .awsAuthenticationCredentials(getAWSAuthenticationCredentials(requestModel)) .lwaAuthorizationCredentials(getLWAAuthorizationCredentials(requestModel)) .awsAuthenticationCredentialsProvider(getAWSAuthenticationCredentialsProvider(requestModel)) .rateLimitConfigurationOnRequests(getRateLimitConfiguration(requestModel)) .endpoint(requestModel.getEndpoint()) .build(); return apiInstance; } /** * 获取财务数据列表 * 请求速率: 默认0.5/s;最大请求数:30 * * @param requestModel * @return */ double permitsPerSecond = 0.5; requestModel.setRateLimitPermit(permitsPerSecond); DefaultApi apiInstance = AmazonSPSellerConfig.getFinancesApiInstance(requestModel); ListFinancialEventsResponse response = apiInstance.listFinancialEvents(maxResultsPerPage, postedAfter, postedBefore, nextToken); 四、生成FBA库存FBAInventory的SDK

操作流程和生成订单Orders的SDK相同,只是目录地址及项目文件夹不同。

复制文件:同订单流程: fba-inventory-api-model,fbaInventory.json

生成项目代码:(java -jar C:\SwaggerToCL\swagger-codegen-cli.jar generate -i C:\SwaggerToCL\fbaInventory.json -l java -t C:\Users\administer\Desktop\selling-partner-api-models-main\clients\sellingpartner-api-aa-java\resources\swagger-codegen\templates\ -o C:\SwaggerToCL\FBAInventorys_JavaCL)。

添加依赖:进入目录C:\SwaggerToCL\FBAInventorys_JavaCL,在pom.xml文件中添加依赖: com.amazon.sellingpartnerapi sellingpartnerapi-aa-java 1.0

打包:同订单流程。

安装:打包成功无报错后,进入target文件夹 ,查看是否有新生成的完整的swagger-java-client-1.0.0.jar。则改jar名为:sellingpartnerapi-aa-java-fbaInventorys-1.0.jar。 (已下载SDK的可直接从该步开始,前面可忽略)然后进入cmd执行命令在本地 Maven 存储库中安装。 (命令:mvn install:install-file -Dfile=sellingpartnerapi-aa-java-fbaInventorys-1.0.jar -DgroupId=com.amazon.sellingpartnerapi -DartifactId=fbaInventorys -Dversion=1.0 -Dpackaging=jar)。安装成功后,在本地maven仓库中对应目录下就能看到该sdk包了(如:C:\Users\administer\.m2\repository\com\amazon\sellingpartnerapi\fbaInventorys\1.0\fbaInventorys-1.0.jar)。

添加依赖:在对应项目的pom.xml中添加maven依赖: com.amazon.sellingpartnerapi fbaInventorys 1.0

代码调用: 示例代码:

/** * Create an instance of the FbaInventory API and call an operation * * @param requestModel * @return * @throws Exception */ public static FbaInventoryApi getFbaInventorysApiInstance(AmazonOwhRequestModel requestModel) throws Exception { FbaInventoryApi apiInstance = new FbaInventoryApi.Builder() .awsAuthenticationCredentials(getAWSAuthenticationCredentials(requestModel)) .lwaAuthorizationCredentials(getLWAAuthorizationCredentials(requestModel)) .awsAuthenticationCredentialsProvider(getAWSAuthenticationCredentialsProvider(requestModel)) .rateLimitConfigurationOnRequests(getRateLimitConfiguration(requestModel)) .endpoint(requestModel.getEndpoint()) .build(); return apiInstance; } /** * 获取FBA库存列表 * 请求速率: 默认2/s;最大请求数:2 * * @param requestModel * @return */ double permitsPerSecond = 2; requestModel.setRateLimitPermit(permitsPerSecond); FbaInventoryApi apiInstance = AmazonSPSellerConfig.getFbaInventorysApiInstance(requestModel); GetInventorySummariesResponse response = apiInstance.getInventorySummaries(granularityType, granularityId, marketplaceIds, details, startDateTime, sellerSkus, nextToken); 五、生成创建Feed文档的SDK

操作流程和生成订单Orders的SDK相同,只是目录地址及项目文件夹不同。

复制文件:同订单流程: feeds-api-model,feeds_2020-09-04.json

生成项目代码:(java -jar C:\SwaggerToCL\swagger-codegen-cli.jar generate -i C:\SwaggerToCL\feeds_2020-09-04.json -l java -t C:\Users\administer\Desktop\selling-partner-api-models-main\clients\sellingpartner-api-aa-java\resources\swagger-codegen\templates\ -o C:\SwaggerToCL\Feeds_20200904_JavaCL)。

添加依赖:进入目录C:\SwaggerToCL\Feeds_20200904_JavaCL,在pom.xml文件中添加依赖: com.amazon.sellingpartnerapi sellingpartnerapi-aa-java 1.0

打包:同订单流程。

安装:打包成功无报错后,进入target文件夹 ,查看是否有新生成的完整的swagger-java-client-1.0.0.jar。则改jar名为:sellingpartnerapi-aa-java-feeds_20200904-1.0.jar。(已下载SDK的可直接从该步开始,前面可忽略) 然后进入cmd执行命令在本地 Maven 存储库中安装。 (命令:mvn install:install-file -Dfile=sellingpartnerapi-aa-java-feeds_20200904-1.0.jar -DgroupId=com.amazon.sellingpartnerapi -DartifactId=feeds_20200904 -Dversion=1.0 -Dpackaging=jar)。安装成功后,在本地maven仓库中对应目录下就能看到该sdk包了(如:C:\Users\administer\.m2\repository\com\amazon\sellingpartnerapi\feeds_20200904\1.0\feeds_20200904-1.0.jar)。

添加依赖:在对应项目的pom.xml中添加maven依赖: com.amazon.sellingpartnerapi feeds_20200904 1.0

代码调用: 示例代码:

/** * Create an instance of the Feeds API and call an operation * * @param requestModel * @return * @throws Exception */ public static FeedsApi getFeedsApiInstance(AmazonSPSellerRequestModel requestModel) throws Exception { FeedsApi apiInstance = new FeedsApi.Builder() .awsAuthenticationCredentials(getAWSAuthenticationCredentials(requestModel)) .lwaAuthorizationCredentials(getLWAAuthorizationCredentials(requestModel)) .awsAuthenticationCredentialsProvider(getAWSAuthenticationCredentialsProvider(requestModel)) .rateLimitConfigurationOnRequests(getRateLimitConfiguration(requestModel)) .endpoint(requestModel.getEndpoint()) .build(); return apiInstance; } /** * 创建Feed文档 * 请求速率: 默认0.0083/s;最大请求数:15 * * @param requestModel * @param contentType * @return * @throws Exception */ private CreateFeedDocumentResponse createFeedDocument(AmazonSPSellerRequestModel requestModel, String contentType) throws Exception { CreateFeedDocumentSpecification body = new CreateFeedDocumentSpecification(); body.setContentType(contentType); double permitsPerSecond = 0.0083; requestModel.setRateLimitPermit(permitsPerSecond); FeedsApi apiInstance = AmazonSPSellerConfig.getFeedsApiInstance(requestModel); CreateFeedDocumentResponse response; try { response = apiInstance.createFeedDocument(body); } catch (ApiException e) { throw new RuntimeException(e.getCode() + ": " + e.getResponseBody()); } return response; } /** * 创建Feed * 请求速率: 默认0.0083/s;最大请求数:15 * * @param requestModel * @param feedType * @param marketplaceIdList * @param inputFeedDocumentId * @param feedOptions * @return * @throws Exception */ private CreateFeedResponse createFeed(AmazonSPSellerRequestModel requestModel, String feedType, List marketplaceIds, String inputFeedDocumentId, FeedOptions feedOptions) throws Exception { CreateFeedSpecification body = new CreateFeedSpecification(); body.setFeedType(feedType); body.setMarketplaceIds(marketplaceIds); body.setInputFeedDocumentId(inputFeedDocumentId); body.setFeedOptions(feedOptions); double permitsPerSecond = 0.0083; requestModel.setRateLimitPermit(permitsPerSecond); FeedsApi apiInstance = AmazonSPSellerConfig.getFeedsApiInstance(requestModel); CreateFeedResponse response; try { response = apiInstance.createFeed(body); } catch (ApiException e) { throw new RuntimeException(e.getCode() + ": " + e.getResponseBody()); } return response; } 六、生成加密上传和下载并解密文档的SDK

操作流程和生成订单Orders的SDK流程略有不同。

打包:进入目录 C:\Users\administer\Desktop\selling-partner-api-models-main,选择clients文件夹并进入,找到项目文件夹sellingpartner-api-documents-helper-java进入。从此处进入cmd, 执行 mvn package 命令打包。 (如果打包报错test部分,在其pom.xml文件中maven-surefire-plugin下添加true,然后再重新打包。) 安装:打包成功无报错后,进入target文件夹 ,查看是否有新生成的完整的sellingpartner-api-documents-helper-java-1.0.0.jar。若有,则改jar名为:sellingpartner-api-documents-helper-java-1.0.jar。 (已下载SDK的可直接从该步开始,前面可忽略)然后进入cmd执行命令在本地 Maven 存储库中安装。 (命令:mvn install:install-file -Dfile=sellingpartner-api-documents-helper-java-1.0.jar -DgroupId=com.amazon.sellingpartnerapi -DartifactId=documents-helper -Dversion=1.0 -Dpackaging=jar)。安装成功后,在本地maven仓库中对应目录下就能看到该sdk包了(如:C:\Users\administer\.m2\repository\com\amazon\sellingpartnerapi\documents-helper\1.0\documents-helper-1.0.jar)。 添加依赖:在对应项目的pom.xml中添加maven依赖: com.amazon.sellingpartnerapi documents-helper 1.0 代码调用: 示例代码: private final UploadHelper uploadHelper = new UploadHelper.Builder().build(); private final DownloadHelper downloadHelper = new DownloadHelper.Builder().build(); /** * 加密并上传 * * @param key * @param initializationVector * @param url * @param documentContent */ public void encryptAndUpload_fromString(String key, String initializationVector, String url, String documentContent) { AESCryptoStreamFactory aesCryptoStreamFactory = new AESCryptoStreamFactory.Builder(key, initializationVector).build(); String contentType = String.format("text/xml; charset=%s", StandardCharsets.UTF_8); // The character set must be the same one that is specified in contentType. try (InputStream source = new ByteArrayInputStream(documentContent.getBytes(StandardCharsets.UTF_8))) { UploadSpecification uploadSpec = new UploadSpecification.Builder(contentType, aesCryptoStreamFactory, source, url).build(); uploadHelper.upload(uploadSpec); } catch (CryptoException | HttpResponseException | IOException e) { // Handle exception. throw new RuntimeException(e.getMessage()); } } /** * 加密并上传 * * @param key * @param initializationVector * @param url * @param documentContent */ public void encryptAndUpload_fromPipedInputStream(String key, String initializationVector, String url, String documentContent) { AESCryptoStreamFactory aesCryptoStreamFactory = new AESCryptoStreamFactory.Builder(key, initializationVector).build(); String contentType = String.format("text/xml; charset=%s", StandardCharsets.UTF_8); try (PipedInputStream source = new PipedInputStream()) { new Thread ( new Runnable() { public void run() { try (PipedOutputStream documentContents = new PipedOutputStream(source)) { // The character set must be the same one that is specified in contentType. documentContents.write(documentContent.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { // Handle exception. throw new RuntimeException(e.getMessage()); } } } ).start(); UploadSpecification uploadSpec = new UploadSpecification.Builder(contentType, aesCryptoStreamFactory, source, url).build(); uploadHelper.upload(uploadSpec); } catch (CryptoException | HttpResponseException | IOException e) { // Handle exception. throw new RuntimeException(e.getMessage()); } } /** * 下载并解密 * * @param key * @param initializationVector * @param url * @param compressionAlgorithm */ public String downloadAndDecrypt(String key, String initializationVector, String url, String compressionAlgorithm) { AESCryptoStreamFactory aesCryptoStreamFactory = new AESCryptoStreamFactory.Builder(key, initializationVector).build(); DownloadSpecification downloadSpec = new DownloadSpecification.Builder(aesCryptoStreamFactory, url) .withCompressionAlgorithm(CompressionAlgorithm.fromEquivalent(compressionAlgorithm)) .build(); StringBuffer sbf = new StringBuffer(); try (DownloadBundle downloadBundle = downloadHelper.download(downloadSpec)) { try (BufferedReader reader = downloadBundle.newBufferedReader()) { String line; do { line = reader.readLine(); // Process the decrypted line. if (null != line) { sbf.append(line); } } while (line != null); } } catch (CryptoException | HttpResponseException | IOException | MissingCharsetException e) { // Handle exception here. throw new RuntimeException(e.getMessage()); } return sbf.toString(); } /** * 加密并上传Feed文档内容 * * @param key * @param initializationVector * @param url * @param standard * @param feedContent */ public String encryptAndUploadFeedDocument(String key, String initializationVector, String url, String standard, String feedContent) throws Exception { try { encryptAndUpload_fromString(key, initializationVector, url, feedContent); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return null; } /** * 下载并解密Feed文档 * * @param key * @param initializationVector * @param url * @param standard * @param compressionAlgorithm * @return */ public String downloadAndDecryptFeedDocument(String key, String initializationVector, String url, String standard, String compressionAlgorithm) throws Exception { String result; try { result = downloadAndDecrypt(key, initializationVector, url, compressionAlgorithm); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return result; } 七、创建Feed文档和加密上传的组合使用示例

代码调用: 示例代码:

String contentType = "text/xml; "; String feedType = "POST_ORDER_FULFILLMENT_DATA"; // TODO 示例,可按需要自行调整 try { // 1.调用 createFeedDocument 操作,为您提交的Feed指定内容类型 CreateFeedDocumentResponse createFeedDocumentResponse = createFeedDocument(requestModel, contentType); if (null != createFeedDocumentResponse && null == createFeedDocumentResponse.getErrors()) { CreateFeedDocumentResult createFeedDocumentResult = createFeedDocumentResponse.getPayload(); if (null != createFeedDocumentResult) { String feedDocumentId = createFeedDocumentResult.getFeedDocumentId(); // Feed文档的标识符,在两天后过期 String url = createFeedDocumentResult.getUrl(); // 用于上传Feed内容的预签名URL。此 URL 将在 5 分钟后过期 FeedDocumentEncryptionDetails encryptionDetails = createFeedDocumentResult.getEncryptionDetails(); // 所需客户端加密和解密文档内容的加密详细信息 if (null != encryptionDetails) { FeedDocumentEncryptionDetails.StandardEnum standard = encryptionDetails.getStandard(); // 加密或解密文档内容所需的加密标准 String initializationVector = encryptionDetails.getInitializationVector(); // 使用密码块链接(CBC)加密或解密文档内容的向量 String key = encryptionDetails.getKey(); // 用于加密或解密文档内容的加密密钥 if (StringUtils.isEmpty(feedDocumentId) || StringUtils.isEmpty(url) || null == standard || StringUtils.isEmpty(initializationVector) || StringUtils.isEmpty(key)) { return null; } // 2.加密并上传Feed文档内容到上一步中的URL String errorMsg = encryptAndUploadFeedDocument(key, initializationVector, url, standard.getValue(), feedContent); if (StringUtils.isNotEmpty(errorMsg)) { // 加密并上传错误 throw new RuntimeException(errorMsg); } // 3.调用 createFeed 操作 CreateFeedResponse createFeedResponse = createFeed(requestModel, feedType, marketplaceIds, feedDocumentId, null); if (null != createFeedResponse && null == createFeedResponse.getErrors()) { CreateFeedResult createFeedResult = createFeedResponse.getPayload(); if (null != createFeedResult) { String feedId = createFeedResult.getFeedId(); // TODO 此处写自己的业务处理逻辑 } } } } } } catch (Exception e) { throw new RuntimeException(e.getMessage()); } 八、已生成好的SDK包(可直接下载使用)

各位小伙伴,如果觉得该文章对你有帮助,请帮忙点个赞,谢谢。

SDK资源下载链接(https://download.csdn.net/download/weixin_42567205/87435724?spm=1001.2014.3001.5501) SDK网盘下载链接(https://pan.baidu.com/s/1VUig3XWV9hx21EFzQmCW0Q?pwd=gmai)



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭